package com.dianrui.operation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dianrui.operation.Constant;
import com.dianrui.operation.R;
import com.dianrui.operation.bean.Scheduling;
import com.dianrui.operation.event.AlarmScheduing;
import com.dianrui.operation.event.RefreshFix;
import com.dianrui.operation.util.JsonUtils;
import com.dianrui.operation.okhttp.XHttpRequest;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleSchedulingMainActivity extends BaseActivity {

    private String mTitles;

    @BindView(R.id.twentfour_hours_number)
    TextView twentfourHoursNumber;
    @BindView(R.id.thiry_sixhours_number)
    TextView thirySixhoursNumber;
    @BindView(R.id.forty_eighthours_number)
    TextView fortyEighthoursNumber;
    @BindView(R.id.seventy_twohours_number)
    TextView seventyTwohoursNumber;
    @BindView(R.id.mainoutside_number)
    TextView mainoutsideNumber;
    @BindView(R.id.nopraking_number)
    TextView noprakingNumber;
    @BindView(R.id.schedule_count)
    TextView scheduleCount;
    @BindView(R.id.title)
    TextView title;



    @Override
    public int getContentViewId() {
        return R.layout.activity_vehiclescheduling_main;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                mTitles = bundle.getString("title");
                title.setText(mTitles);
            }
        }
        getScheduling();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AlarmScheduing alarmScheduing) {
        getScheduling();
    }

    private void getScheduling() {
        XHttpRequest.getInstance().postRequests(SharedUtil.getUrl() + Url.SCHEDULING, "", new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        if (isFinishing()) {
                            return;
                        }
                        String status= JsonUtils.getJsonStr(result,Constant.STATUS);
                        if (Constant.DATASUCCESS.equals(status)) {
                            String data= JsonUtils.getJsonStr(result,"data");
                            if (!StringUtils.isEmpty(data)) {
                                Scheduling scheduling = JsonUtils.jsonStrToBean(data, Scheduling.class);
                                if (scheduling != null) {
                                    twentfourHoursNumber.setText(scheduling.getOne_day() + "台");
                                    thirySixhoursNumber.setText(scheduling.getTwo_day() + "台");
                                    fortyEighthoursNumber.setText(scheduling.getTwo_days()  + "台");
                                    seventyTwohoursNumber.setText(scheduling.getThree_days() + "台");
                                    mainoutsideNumber.setText(scheduling.getArea_count() + "台");
                                    noprakingNumber.setText(scheduling.getNetwork_count() + "个");
                                    scheduleCount.setText(scheduling.getSchedule_count() + "台");
                                }
                            }
                        }else if ("1001".equals(status)) {
                            ToastUtil.showToast(JsonUtils.getJsonStr(result, "message"));
                        } else if ("1002".equals(status)) {
                            spUtils.clear();
                            ToastUtil.showToast(JsonUtils.getJsonStr(result, "message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {

            }
        });
    }

    @Override
    public void initData() {}


    @OnClick({R.id.back, R.id.layout1, R.id.layout2, R.id.layout3, R.id.layout4, R.id.layout5, R.id.layout6})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.layout1:
                Intent intent = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("titles", getString(R.string.twentfour_hours));
                bundle.putString("type", "2");
                bundle.putString("number", twentfourHoursNumber.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.layout2:
                Intent intent1 = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putString("titles", getString(R.string.thiry_sixhours));
                bundle1.putString("type", "3");
                bundle1.putString("number", thirySixhoursNumber.getText().toString());
                intent1.putExtras(bundle1);
                startActivity(intent1);
                break;
            case R.id.layout3:
                Intent intent2 = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingActivity.class);
                Bundle bundle2 = new Bundle();
                bundle2.putString("titles", getString(R.string.forty_eighthours));
                bundle2.putString("type", "4");
                bundle2.putString("number", fortyEighthoursNumber.getText().toString());
                intent2.putExtras(bundle2);
                startActivity(intent2);

                break;
            case R.id.layout4:
                Intent intent3 = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingActivity.class);
                Bundle bundle3 = new Bundle();
                bundle3.putString("titles", getString(R.string.seventy_twohours));
                bundle3.putString("type", "5");
                bundle3.putString("number", seventyTwohoursNumber.getText().toString());
                intent3.putExtras(bundle3);
                startActivity(intent3);
                break;
            case R.id.layout5:
                Intent intent4 = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingActivity.class);
                Bundle bundle4 = new Bundle();
                bundle4.putString("titles", getString(R.string.mainoutside_txt));
                bundle4.putString("type", "6");
                bundle4.putString("number", mainoutsideNumber.getText().toString());
                intent4.putExtras(bundle4);
                startActivity(intent4);
                break;
            case R.id.layout6:
                Intent intent5 = new Intent(VehicleSchedulingMainActivity.this, VehicleSchedulingAlarmActivity.class);
                Bundle bundle5 = new Bundle();
                bundle5.putString("titles", getString(R.string.main_no_parking));
                bundle5.putString("number", noprakingNumber.getText().toString());
                intent5.putExtras(bundle5);
                startActivity(intent5);
                break;
            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
